﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Globalization;
using System.Drawing;
using System.IO;

namespace VTS.Common
{
    public class Utility
    {
        /// <summary>
        /// Compare string ordinal ignore case
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        public static bool CompareString(string s1, string s2)
        {
            if (s1 == null)
                s1 = string.Empty;
            if (s2 == null)
                s2 = string.Empty;

            return s1.Trim().Equals(s2.Trim(), StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// validate user has logged in system or not
        /// </summary>
        /// <returns></returns>
        public static bool HasLoggedIn()
        {
            return HttpContext.Current.Session[Constants.SESSION_LOGGEDIN] != null ? true : false;
        }


        /// <summary>
        /// this function used to rewriteURL
        /// </summary>
        /// <param name="inputText"></param>
        /// <returns></returns>
        public static string RemoveUnicode(string inputText)
        {
            string stFormD = inputText.Normalize(System.Text.NormalizationForm.FormD);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string str = string.Empty;

            for (int i = 0; i <= stFormD.Length - 1; i++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
                if (uc == UnicodeCategory.NonSpacingMark == false)
                {
                    if (stFormD[i] == 'đ')
                        str = "d";
                    else if (stFormD[i] == 'Đ')
                        str = "D";
                    else if (stFormD[i] == ' ' || stFormD[i] == ' ')
                        str = "-";
                    else
                        str = stFormD[i].ToString();
                    sb.Append(str);
                }
            }

            return sb.ToString();
        }

        public static bool AllowedUpload(string fileName)
        {
            string[] extensions = Constants.ALLOWING_EXTENSION.Split(',');
            string extension = fileName.Substring(fileName.LastIndexOf('.') + 1, fileName.Length - fileName.LastIndexOf('.') - 1);

            return extensions.Contains(extension);
        }


        public static void SaveImage( Stream imageStream, string mapPath)
        {

            using (Bitmap originalBitmap = Bitmap.FromStream(imageStream) as Bitmap, newbmp = new Bitmap(400, 600))
            {
                double WidthVsHeightRatio = (float)originalBitmap.Width / (float)originalBitmap.Height;

                using (Graphics newg = Graphics.FromImage(newbmp))
                {
                    newg.Clear(Color.White);
                    newg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    if (WidthVsHeightRatio == 1d)
                    {
                        newg.DrawImage(originalBitmap, 0, 0, 100, 100);
                        newg.Save();
                    }

                    else if (WidthVsHeightRatio < 1d) //Image is taller than wider
                    {
                        newg.DrawImage(originalBitmap, new RectangleF(new PointF((float)(50 - ((100 * WidthVsHeightRatio) / 2)), 0), new SizeF((float)(100 * WidthVsHeightRatio), 100f)));
                        newg.Save();
                    }

                    else //Image is wider than taller
                    {
                        double inverse = Math.Pow(WidthVsHeightRatio, -1);
                        newg.DrawImage(originalBitmap, new RectangleF(new PointF(0, (float)(50 - ((100 * inverse) / 2))), new SizeF(100f, (float)(100 * inverse))));
                        newg.Save();
                    }
                }

                newbmp.Save(mapPath);
            }
        }
    }
}
